<?php

require_once dirname(__FILE__).'/../lib/requestsGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/requestsGeneratorHelper.class.php';

/**
 * requests actions.
 *
 * @package    project_midnight
 * @subpackage requests
 * @author     AlvaroLara
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class requestsActions extends autoRequestsActions
{
  public function executeIndex(sfWebRequest $request)
  {
    if($this->getUser()->isAuthenticated() && (strcmp($this->getUser()->getUsername(),'alvaro') == 0))
    {
      return parent::executeIndex($request);
    }
    return $this->forward('idea','index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The request was created successfully. Once everything is ready to beta version, i will email you.' : 'The item was updated successfully.';

      $Requests = $form->save();

      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $Requests)));

      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');

        $this->redirect('@requests_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);

        $this->redirect('@idea');
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The request has not been saved due to some errors.', false);
    }
  }

}
